//
//  MineController.m
//  TrainTree
//
//  Created by wanghui on 2017/8/24.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "MineController.h"

#import "MineModel.h"
#import "LoginController.h"
#import "MineHeadCell.h"
#import "StudyRecordCell.h"
#import "RankingCell.h"
#import "MenuCell.h"
#import "TeamManageController.h"
#import "MyPowerController.h"
#import "StudyRecordController.h"
#import "SettingController.h"
#import "MineRequestManager.h"
@interface MineController ()<UITableViewDelegate,UITableViewDataSource,MenuCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**排名数组*/
@property(nonatomic,strong)NSArray *rankingList;
/*部门名称**/
@property(nonatomic,copy)NSString *branchName;
/**学习记录*/
@property(nonatomic,strong)NSDictionary *studyRecord;
/**用户头像*/
@property(nonatomic,copy)NSString *userAvatar;
/**用户名字*/
@property(nonatomic,copy)NSString *userName;
/**是否有下级*/
@property(nonatomic,copy)NSString *hasLowerLevel;


@end

@implementation MineController
- (void)viewWillAppear:(BOOL)animated
{
    if (![Session isLogin]) {
        [self.tableView reloadData];
        [Login presentLoginPage];

    }else{
        [self requestUserSummary];
    }


}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationView.hidden = YES;
    _tableView.delegate = self;
    _tableView.dataSource = self;
 
//    [self requestUserSummary];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadUserData) name:@"reloadUserData" object:nil];
}
- (void)reloadUserData
{
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"reloadUserData" object:nil];
}

- (void)requestUserSummary
{
    WeakSelf(ws);
    [MineRequestManager requestMineSummaryResponse:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
        if ([JSONData[@"c"] intValue] == 0) {
            ws.rankingList   = JSONData[@"d"][@"rankingList"];
            ws.studyRecord   = JSONData[@"d"][@"studyRecord"];
            ws.branchName    = JSONData[@"d"][@"branchName"];
            ws.userAvatar    = JSONData[@"d"][@"userAvatar"];
            ws.userName      = JSONData[@"d"][@"userName"];
            ws.hasLowerLevel = [NSString stringWithFormat:@"%@", JSONData[@"d"][@"hasLowerLevel"]];

            [ws.tableView reloadData];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return ScreenWidth * 212 / 375;
            break;
        case 1:
            return 117;
            break;
//        case 2:
//            return 234;// 204
//            break;
        case 2:
            return 134;
            break;

        default:  return 100;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MineHeadCell *cell = [LoadXibTool loadViewFromXibName:@"MineHeadCell"];
        if([Session isLogin]){
            [cell configWithImgUrl:self.userAvatar userName:self.userName companyName:self.branchName];
        }else{
            [cell configWithImgUrl:nil userName:@"未登录" companyName:@"未登录"];
        }

        return cell;
    } else if (indexPath.row == 1) {
        StudyRecordCell *cell = [LoadXibTool loadViewFromXibName:@"StudyRecordCell"];
        if (self.studyRecord) {
            [cell configWithDic:self.studyRecord];
        }else{
            [cell configWithDic:nil];
        }
        return cell;
    }
//    else if (indexPath.row == 2) {
//        RankingCell *cell = [LoadXibTool loadViewFromXibName:@"RankingCell"];
//        [cell configWithArray:self.rankingList];
//        return cell;
//    }
    else {
        MenuCell *cell = [LoadXibTool loadViewFromXibName:@"MenuCell"];
        cell.delegate = self;
        cell.hasLowerLevel = self.hasLowerLevel;
        
        return cell;
    }
}
#pragma mark - menuCell的代理方法，根据类型跳转
- (void)teamPage:(MenuType)menuType
{
    if (![Session isLogin]) {
        [Login presentLoginPage];
        return ;
    }
    if (menuType == MenuTypeTeam) {
        TeamManageController *teamVc = [[TeamManageController alloc]init];
        teamVc.icon = self.userAvatar;
        teamVc.userName = self.userName;
        teamVc.companyName = self.branchName;
        [self pushToSubPage:teamVc];
    } else if (menuType == MenuTypePower) {
        MyPowerController *powerVc = [[MyPowerController alloc]init];
        [self pushToSubPage:powerVc];

    } else if (menuType == MenuTypeRecord) {
        StudyRecordController *recordVc = [[StudyRecordController alloc]init];
        [self pushToSubPage:recordVc];
    } else {
        SettingController *settingVc = [[SettingController alloc]init];
        settingVc.iconUrl = self.userAvatar;
        [self pushToSubPage:settingVc];
    }
}

- (void)pushToSubPage:(UIViewController *)subVc
{
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:subVc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}



- (IBAction)checkStorage:(id)sender {
//    MineModel *model = [MineModel mj_objectWithKeyValues:[USERDEFAULT objectForKey:@"mineModel"]];
    NSLog(@"------%@", [Session shareSession].userId);
//    NSLog(@"model = %@",model);
    
}
 

- (IBAction)login:(id)sender {
//    
//    [LoginRequestManager loginWithMobile:@"18511663779" Code:@"1234" response:^(id JSONData, NSError *error, NSURLSessionDataTask *dataTask) {
//            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
//
//        [USERDEFAULT setObject:JSONData[@"d"] forKey:@"mineModel"];
//
//        NSDictionary *userInfo = @{
//                                   @"userId":JSONData[@"d"][@"userId"],
//                                   @"userName":JSONData[@"d"][@"userName"],
//                                   @"companyName":JSONData[@"d"][@"companyName"],
//                                   };
//
//        [Session session:userInfo];
//
//    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
